package de.unistuttgart.iaas.interaction.skype;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.eaio.uuid.UUID;

import de.unistuttgart.iaas.interaction.skype.database.MySQLAccess;
import de.unistuttgart.iaas.interaction.skype.util.ConfigFileHandler;
import de.unistuttgart.iaas.interaction.skype.util.TimeoutComputer;

public class SkypeGetRequest {
	
	private static String configurationData = "C:\\CommunicationService\\configuration.conf";
	
	public static String skypeGetRequest(String messageType, String message, String username, 
			String reply, String responseTemplate, String timeout)
	{
		boolean parameterFlag = true;
		String[] attachmentList = null;
		String fail = "";
    	Connection conn = null;
		UUID u = new UUID();
		Calendar cal = Calendar.getInstance();
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  			
		String sentTime = df.format(cal.getTime());
		String newtime="";
		TimeoutComputer tom = new TimeoutComputer();
		String responseTemplateString = "";
		
		ConfigFileHandler config = new ConfigFileHandler(configurationData);
		MySQLAccess dao = new MySQLAccess();
		String admin = config.getSkype_username();
		String table_sentFileMessage = config.getSkydb_table_sentFileMessage();
		String table_sentTextMessage = config.getSkydb_table_sentTextMessage();
		
		if(!messageType.equalsIgnoreCase("text")&&!messageType.equalsIgnoreCase("file"))
		{
			parameterFlag = false;
			fail = "False messageType. ";
		}
		if(message.equals("")||message.equals(" ")||message.equals(null))
		{
			parameterFlag = false;
			fail = "False messagebody. " + fail;
		}
		if(timeout!=null&&!timeout.equals("0")&&!timeout.equals(""))
 		{
			if(timeout.split(":").length==4)
			{
				for(int i=0; i<timeout.split(":").length;i++)
				{
					String[] temp=timeout.split(":");
					try{
						
						Integer.parseInt(temp[i] );
						
					}catch (NumberFormatException e)
					{
						parameterFlag = false;
						fail = "False timeout. " + fail;
					}					
				}
				newtime= tom.setTimeOut(timeout); 
			}else{
				
				parameterFlag = false;
				fail = "False timeout. " + fail;
				}
 		}else{
 				newtime= null;
 		}
		
		if(reply.equals(""))
		{
			parameterFlag =false;
			fail = " Response parameter can not be empty. " + fail;
			
		}else{
			
			if(reply.equalsIgnoreCase("yes"))
			{
				if(responseTemplate==null||responseTemplate.equals(""))
				{
					parameterFlag =false;
					fail = "responseTemplate parameter can not be empty. " +fail;
				}else
				{
					//responseTemplate definition
					
				}
			}
			else if(reply.equalsIgnoreCase("no"))
			{
			
			}else
			{
				parameterFlag =false;
				fail = "False reply. "+ fail;
			}
	}

	if( parameterFlag ==false)
	{
		System.out.println(fail);
		return fail;		
	}
	 try
	 {
    		conn = dao.connDataBase(config);
    	
    		if(conn != null)
    		{	    		 
    			Statement reg_statement = conn.createStatement();
    			ResultSet rs = reg_statement.executeQuery("select * from skydb."+ config.getSkydb_table_register()+" where skype_username='"+username+"'");
    			if(rs.next())
    			{
    				if(messageType.equalsIgnoreCase("text"))
    				{
    					PreparedStatement preStatement = conn.prepareStatement("insert into skydb."+ table_sentTextMessage +" values(?,?,?,?,?,?,?,?,?)");
    					
    		 			preStatement.setString(1, u.toString());
    		 			preStatement.setString(2, username);
    		 			preStatement.setString(3, admin);
    		 			preStatement.setString(4, message);
    		 			preStatement.setString(5, sentTime);
    		 			preStatement.setString(6, reply);
    		 			preStatement.setString(7, responseTemplate);
    		 			preStatement.setString(8, newtime);		 			
    		 			preStatement.setString(9, "NEW");
    		 			preStatement.executeUpdate();
    		 			preStatement.close();
    				}else if(messageType.equalsIgnoreCase("file"))
    				{
    					PreparedStatement preStatement = conn.prepareStatement("insert into skydb."+ table_sentFileMessage +" values(?,?,?,?,?,?,?,?,?)");
    		 			preStatement.setString(1, u.toString());
    		 			preStatement.setString(2, username);
    		 			preStatement.setString(3, admin);
    		 			preStatement.setString(4, message);
    		 			preStatement.setString(5, sentTime);
    		 			preStatement.setString(6, reply);
    		 			preStatement.setString(7, responseTemplate);
    		 			preStatement.setString(8, newtime);		 			
    		 			preStatement.setString(9, "NEW");
    		 			preStatement.executeUpdate();
    		 			preStatement.close();
    					
    				}else 
    				{
    					System.out.println("false messageType. " + fail);
    					return fail;
    				}
    				
		 	    	if(reply.equalsIgnoreCase("yes"))
		 	    	{
		 	    		return u.toString();
		 	    	}else
		 	    	{
		 	    		return "The request is accepted.";
		 	    	}
    			}else
    			{
    				return "Unregister";
    				//unregister user
    			}
    		}else
    		{
    			return "Database connection error.";
    		}
	 }catch(SQLException e)
      {
       		System.out.println(fail);
       		return "SQL Error.";
//       		 return reason_sql+fail;
       		 //errorHandler
       		 
       	 }catch(Exception e)
       	 {
       		 System.out.println(fail);
   			 return "ERROR";
//       		 return fail;
       	 }		
	}


}
